//
//  UIImageView+Utils.m
//  KKMY2_U
//
//  Created by 黄磊 on 14-8-6.
//  Copyright (c) 2014年 jiulong.zhou. All rights reserved.
//

#import "UIImageView+Utils.h"

@implementation UIImageView (Utils)

- (void)setImageUrl:(NSString *)imageUrl placeholderImage:(UIImage *)placeholderImage
{
    if (imageUrl == nil || [imageUrl isEqualToString:@""])
    {
        if (![[NSThread currentThread] isMainThread]) {
            LogError(@"在非主线程中");
        }
        [self setImage:placeholderImage];
        return;
    }
    
    NSString *remoteFilePath = [NSString isFullUrl:imageUrl];
    
    BOOL hasCache = [CacheManager getFileWithUrl:remoteFilePath
                                       fileType:eCacheFileImage
                                       useCache:YES
                                     completion:^(BOOL isSucceed, NSString *message, NSObject *data)
    {
        if (isSucceed) {
            if (data) {
                [self setImage:(UIImage *)data];
            }
        } else {
            [self setImage:placeholderImage];
        }
    }];
    
    if (!hasCache) {
        [self setImage:placeholderImage];
    }
}

@end
